home *** CD-ROM | disk | FTP | other *** search
- /* Passing our first variable. */
- using System;
-
- namespace Chapter2 {
- class Class1 {
- static void Main() {
- string input;
- float number = 0, root = 0;
-
- Console.WriteLine("This program will find the "
- + "square root of any number\n"
- + "To continue just enter a number when prompted\n"
- + "Your results will appear after you press enter\n"
- + "To quit just enter the number \"1\"\n"
- + "Enter your first value now: ");
- input = Console.ReadLine();
- number = float.Parse(input);
-
- while (number > 1) {
- root = SquareRoot(number);
- Console.WriteLine(root);
-
- Console.WriteLine("Enter your next value now: ");
- input = Console.ReadLine();
- number = float.Parse(input);
- }
- }
-
- // This function generates square roots using Newton's method
- static float SquareRoot(float n) {
- float test1, test2;
- test1 = n - (float).01 - ((n - (float).01) * (n - (float).01) - n)
- / (n * (n - (float).01));
- test2 = test1 - ((test1 * test1) - n) / ((n) * test1);
-
- while (test1 != test2) {
- test1 = test2;
- test2 = test1 - (test1 * test1 - n) / ((n) * test1);
- }
- return (test2);
- }
- }
- }
-
-